﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using Pulsar.ClienteWeb.Dominio;


namespace Pulsar.ClienteWeb
{
    public partial class Sesion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void cmdIngresar_Click(object sender, EventArgs e)
        {
            //Prueba de obtencion de cliente via HTTP GET
            string cadena = "http://localhost:2791/Sesiones.svc/Sesiones/" + txtUsuario.Text + "/" + txtPassword.Text;
            HttpWebRequest req = (HttpWebRequest)WebRequest.
                Create(cadena);
            req.Method = "GET";
            HttpWebResponse res = null;


            try
            {
                res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string usuarioJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                Usuario usuarioObtenido = js.Deserialize<Usuario>(usuarioJson);
                //Response.Write("<script language=javascript>alert('Bienvenido! ...Redireccionar a la página de Registrar Reserva');</script>");
                Response.Redirect("http://localhost:3232/wfConsultar.aspx");

            }

            catch (WebException ex)
            {
                HttpWebResponse resError = (HttpWebResponse)ex.Response;
                StreamReader reader = new StreamReader(resError.GetResponseStream());
                string error = reader.ReadToEnd();

                JavaScriptSerializer js = new JavaScriptSerializer();
                Error errorMessage = js.Deserialize<Error>(error);
                Response.Write("<script language=javascript>alert('" + errorMessage.MensajeError + "');</script>");
            }

        }
    }
}